#include "qt_ubutu_serial.h"

//TCP通信协议
int ubuntu_socket_create()
{
    //创建套接字
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
    {
        ERR_MSG("socket");
        return 0;
    }
    printf("创建套接字成功\n");
    // 允许端口快速重用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {
        ERR_MSG("setsockopt");
        return -1;
    }
    printf("允许端口快速重用\n");
    // 绑定信息结构体
    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    socklen_t addrlen = sizeof(sin);
    /*绑定*/
    if (bind(sfd, (struct sockaddr *)&sin, addrlen) < 0)
    {
        ERR_MSG("bind");
        return -1;
    }
    printf("bind success\n");
    /*监听*/
    if (listen(sfd, 128) < 0)
    {
        ERR_MSG("listen");
        return -1;
    }
    printf("listen success\n");
}
